package com.dckeji.shiplock.webapp.controller.rest;

import com.dckeji.shiplock.framework.constants.SystemConstants;
import com.dckeji.shiplock.webapp.controller.BaseController;
import com.dckeji.shiplock.webapp.pojo.Examine;
import com.dckeji.shiplock.webapp.pojo.LogicRsult;
import com.dckeji.shiplock.webapp.pojo.Manager;
import com.dckeji.shiplock.webapp.pojo.Route;
import com.dckeji.shiplock.webapp.service.IManagerService;
import com.dckeji.shiplock.webapp.service.IOwnerService;
import com.dckeji.shiplock.webapp.service.IPayService;
import com.dckeji.shiplock.webapp.util.ReturnMsgUtil;
import com.lkx.util.ExcelUtil;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUpload;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: TianWei
 * Date: 2017/8/14
 * Time: 16:27
 */

@RestController("restController")
@RequestMapping("/rest/ma/")
public class ManagerController extends BaseController {

    @Autowired
    IPayService payService;
    @Autowired
    IManagerService managerService;

    @Autowired
    IOwnerService ownerService;


    @RequestMapping("login")
    public LogicRsult managerLogin(@RequestBody Manager manager) {
        if (valida(manager)) {
            String ipAddr = getIpAddr();
            manager.setManagerLoginIp(ipAddr);
            Manager manager1 = managerService.managerLogin(manager);
            session.setAttribute("login", manager1);
            if (null != manager1) {
                return ReturnMsgUtil.success(manager1);
            } else {
                return ReturnMsgUtil.busError("用户名或密码错误!");
            }

        } else {
            return ReturnMsgUtil.valiError();
        }
    }

    /**
     * 审核
     *
     * @param examine 1 缴费退费 2 过闸申报  3 延时过号
     * @return
     */
    @RequestMapping("examine")
    public LogicRsult examine(@RequestBody Examine examine) {
        int row = managerService.examine(examine);
        if (row > 0) {
            List<Route> routes = managerService.queryRouteByShipNo(examine.getShipNo());
            LogicRsult logicRsult = managerService.rejectPush(routes, examine.getShipNo(), examine.getOptType(), examine.getRejectDesc(), examine.getExamineType());
            return logicRsult;
        } else {
            return ReturnMsgUtil.sysError();
        }
    }


    @RequestMapping("uploadData")
    public LogicRsult upLoadData() {
        DiskFileItemFactory diskFileItemFactory = new DiskFileItemFactory();
        ServletFileUpload upload = new ServletFileUpload(diskFileItemFactory);
        upload.setFileSizeMax(1024 * 500);
        upload.setHeaderEncoding("UTF-8");
        diskFileItemFactory.setSizeThreshold(1024 * 500);
        try {
            List<FileItem> fileItems = upload.parseRequest(request);
            int row = managerService.upLoadData(fileItems);
            if (row == Integer.valueOf(SystemConstants.ResultCode.SUCCESS)) {
                return ReturnMsgUtil.success();
            } else {
                return ReturnMsgUtil.sysError();
            }

        } catch (FileUploadException e) {
            e.printStackTrace();
        }
        return ReturnMsgUtil.sysError();

    }
}
